package com.dfbz.controller;


import com.dfbz.service.UserService;
import com.dfbz.service.deptService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/dept/*")
public class deptServlet extends baseServlet {
    public deptService deptservice = new deptService();
    private UserService userService = new UserService();

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询全部部门 信息  部门人数... id
        List<Map<String, Object>> deptMapList = deptservice.findAll();
        for (Map<String, Object> dept : deptMapList) {

            // 根据id查询下属用户(因为部门列表页面只需要用户id、用户姓名、是否私密等参数,我们不需要把整个user都查询出来)
            Long deptId = Long.parseLong(dept.get("id").toString());

            List<Map<String, Object>> userMapList = userService.findUserByDeptId(deptId);

            dept.put("userMapList", userMapList);
        }
        request.setAttribute("deptMapList", deptMapList);
        request.getRequestDispatcher("/html/department.jsp").forward(request, response);
    }

    public void findAllDept(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String, Object>> deptMapList = deptservice.findAll();
        writerObjToString(response, deptMapList);
    }
}
